package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemMenu;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.SystemMenuObjectQuery;
import cn.wolfcode.crm.service.ISystemMenuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    @Autowired
    private ISystemMenuService systemMenuService;

    //跳转到菜单主页
    @RequiresPermissions("systemMenu:index")
    @RequestMapping("index")
    public String index(){
        return "systemMenu/list";
    }

    @RequiresPermissions("systemMenu:list")
    @PermissionName("系统菜单主页")
    @RequestMapping("list")
    @ResponseBody
    public  Map<String , Object> list( @ModelAttribute("qo") SystemMenuObjectQuery qo) {
        PageResult<SystemMenu> query = systemMenuService.query(qo);
        Map<String , Object> result = new HashMap<>();
        result.put("rows",query.getRows());
        return result;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public PageResult<SystemMenu> selectAll(Model model, @ModelAttribute("qo") ObjectQuery qo) {
        List<SystemMenu> list = systemMenuService.getAll();
        return new PageResult<>(list.size(),list);
    }


    //用于查询父菜单
    @RequestMapping("getParentMenu")
    @ResponseBody
    public List<SystemMenu> getParentMenu(@ModelAttribute("qo") SystemMenuObjectQuery qo){

        PageResult<SystemMenu> query = systemMenuService.query(qo);
        List<SystemMenu> rows = query.getRows();
        return rows;
    }

    @RequiresPermissions("systemMenu:delete")
    @PermissionName("系统菜单删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                systemMenuService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除系统菜单失败");
        }
        return jsonResult;
    }



    @RequiresPermissions("systemMenu:saveOrUpdate")
    @PermissionName("系统菜单创建更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Model model, SystemMenu systemMenu) {
        JsonResult json = new JsonResult();
        try {

        if (systemMenu.getId() != null) {
            systemMenuService.update(systemMenu);
        } else {
            systemMenuService.save(systemMenu);
        }
        }catch (Exception e){
            json.markErrorMsg("更新失败");
        }
        return json;
    }

   /* @RequiresPermissions("systemMenu:loadMenuByParentSn")
    @PermissionName("系统菜单加载")*/
    @ResponseBody
    @RequestMapping("loadMenuByParentSn")
    public List<Map> loadMenuByParentSn(Model model, String parentSn) {
        List<Map> list = systemMenuService.selectMenuByParentSn(parentSn);
        return list;
    }


    @RequestMapping("queryMenusByRoleId")
    @ResponseBody
    public PageResult<SystemMenu> queryPermissionByRoleId(Long roleId) {
        PageResult<SystemMenu> list = systemMenuService.queryMenusByRoleId(roleId);
        return list;
    }

}
